编写一个递归函数求满足1^2+2^2+…+n^2<1000的最大的n。

来源:百度知道 编辑:UC知道 时间:2024/06/23 04:43:59
急用,在线等解答!谢谢!!

#include <stdio.h>
void fun(int n)//自定义函数
{
static int s=0;//s存放和
s+=n*2;
if(s>=1000)//判断条件
{
printf("%d",n-1);//输出最大
return;
}
else
fun(n+1);//递归
}
main()
{
fun(1);
}

#include "stdio.h"

void f(int n)
{
static int s=0;
s+=n*n;
if(s>=1000)
{
printf("%d",n-1);
return;
}
else
f(n+1);
}

void main()
{
f(1);
}